package com.mystyle.le.qjnewsproject.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * 下载数据
 * Created by le on 2016/10/8.
 */
public class DownData {



    public static byte[] downTitles(String path){
        HttpURLConnection coon = null;
        ByteArrayOutputStream outputStream = null;
        try {
            URL url = new URL(path);
            coon = ((HttpURLConnection) url.openConnection());
            coon.setRequestMethod("GET");
            coon.setReadTimeout(5*1000);
            coon.connect();
            if (coon.getResponseCode()==200){
                InputStream inputStream = coon.getInputStream();
                byte[] buf = new byte[1024];
                int len = 0;
                while ((len=inputStream.read(buf))!=-1){
                    outputStream.write(buf,0,len);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (coon!=null){
                    coon.disconnect();
                }
            }
        }
        return outputStream.toByteArray();
    }
}
